package lan.battletank.element;

import lan.battletank.R;
import lan.battletank.environment.EnvConst;
import lan.battletank.environment.Stage;
import lan.battletank.logic.ElementStatusLogic;
import lan.battletank.logic.EnemyLogic;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.drawable.BitmapDrawable;

public class EnemyEntity extends BaseBattleUnitElement {

    /** Tank's movement speed. */
    private final float MOVE_SPEED = 80;

    /** Tank's cannon angle adjust speed(degrees/sec). */
    private final int CANNON_ADJUST_SPEED = 18;

    /** Tank's cannon angle bounds */
    private final int CANNON_HIGH = 90;
    private final int CANNON_LOW = 0;

    /** Tank image to use */
    protected BitmapDrawable mTankImage;

    private Paint p = new Paint();

    /** Constructor, initial all parameters here. */
    public EnemyEntity(Context context) {
        mContext = context;
        Resources res = mContext.getResources();
        mTankImage = new BitmapDrawable(Bitmap.createScaledBitmap(BitmapFactory.decodeResource(res, R.drawable.tank_enemy), TANK_WIDTH, TANK_HEIGHT, true));
        isPlayer = false;
        width = TANK_WIDTH;
        height = TANK_HEIGHT;
        halfWidth = width / 2;
        halfHeight = height / 2;

        // Fire effect
        for (int i = 0; i < ALL_PHASE; i++) {
            mFireImages[i] = BitmapFactory.decodeResource(res, R.drawable.fire_01 + i);
        }
        mFireWidth = mFireImages[0].getWidth();
        mFireHeight = mFireImages[0].getHeight();

        // Initialize default value.
        initialize();

        p.setColor(Color.RED);
        p.setStyle(Style.FILL);
        p.setTextSize(20);
    }

    public void draw(Canvas c) {
    	super.draw(c, mTankImage, p);
    }

    public void setPositionAndInitialize(Stage stage, int canvasWidth, int canvasHeight) {
        initialize();
        centerX = canvasWidth - width;
        centerY = stage.getEnemyInitialCenterY(this);
    }

    public void startFiring(EnemyBulletEntity bullet) {
        mIsFiring = true;
        gunX = (int) centerX - halfWidth;
        gunY = (int) centerY - 20;
        mShowFireEffect = true;
        bullet.setFire(gunX, gunY);
    }

    public void actionEnd() {
        super.actionEnd();
        EnemyLogic.inAction = false;
    }

    public void action(ControlPanelEntity controlPanel, BaseBattleUnitElement target, Stage stage, int canvasWidth, int canvasHeight, double timeInterval) {
        if (isAlive) {
            stage.tankFallCheck(this);
            if (isFalling) {
                if (isFallingR) {
                    centerX += 1;
                } else if (isFallingL) {
                    centerX -= 1;
                } else {
                    centerY += 1;
                }
                // stage.tankFallCheck(this);
            } else {
            	if (isMoving) {
                    // check if the tank is reach the screen edge
                    if (centerX > halfWidth && controlPanel.goLeft() && stage.canGoLeft(this)) {
                        centerX -= timeInterval * MOVE_SPEED;
                    } else if (centerX < (canvasWidth - halfWidth) && controlPanel.goRight() && stage.canGoRight(this)) {
                        centerX += timeInterval * MOVE_SPEED;
                    }
                    // check the floor
                    // stage.tankFallCheck(this);
                    // target collision check
                    if (ElementStatusLogic.isColliding(this, target)) {
                        if (controlPanel.goLeft()) {
                            centerX = target.centerX + target.halfWidth + halfWidth;
                        } else if (controlPanel.goRight()) {
                            centerX = target.centerX - target.halfWidth - halfWidth;
                        }
                    }
                }
            }
            if (isAdjusting) {
                if (mCannonAngle < CANNON_HIGH && controlPanel.angleUp()) {
                    mCannonAngle += timeInterval * CANNON_ADJUST_SPEED;
                } else if (mCannonAngle > CANNON_LOW && controlPanel.angleDown()) {
                    mCannonAngle -= timeInterval * CANNON_ADJUST_SPEED;
                } else if (mCannonAngle > 90) {
                    mCannonAngle = 90;
                } else if (mCannonAngle < 0) {
                    mCannonAngle = 0;
                }
            }
            if (mShowFireEffect) {
                if (mPhase == 0) {
                    mPhase++;
                }
                if (mPeriodCounter > PERIOD) {
                    if (mPhase < ALL_PHASE) {
                        mPeriodCounter = 1;
                        mPhase++;
                    } else {
                        mShowFireEffect = false;
                        mPhase = 0;
                        mPeriodCounter = 0;
                    }
                } else {
                    mPeriodCounter++;
                }
            }
        }
    }

    
    public void setPreparingToFire(boolean b) {
        prepareToFire = b;
    }

    public boolean isPreparingToFire() {
        return prepareToFire;
    }

    protected void initialize() {
    	hp = EnvConst.HP;
        gunX = 0;
        gunY = 0;
        isAlive = true;
        mIsFiring = false;
        prepareToFire = false;
        isMoving = false;
        isAdjusting = false;
        isFalling = false;
        leftStable = true;
        rightStable = true;
        isFallingL = false;
        isFallingR = false;
        mCannonAngle = 0;
        mShowFireEffect = false;
        mPhase = 0;
        mPeriodCounter = 0;
    }
}
